package com.itheima.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.itheima.entity.Orders;
import com.itheima.service.IOrdersService;
import com.itheima.util.Result;
import com.itheima.util.UserHolder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

@Slf4j
@RestController
@RequestMapping("/order")
public class OrdersController {

    @Autowired
    private IOrdersService ordersService;

    @PostMapping("/submit")
    public Result submit(@RequestBody Orders orders){

        return ordersService.submit(orders);
    }

    @GetMapping("/page")
    public Result getPage(@RequestParam(value = "page") int page, @RequestParam(value = "pageSize")  int pageSize, @RequestParam(value = "number" ,required = false) Long number){
        IPage ordersServicePage = ordersService.getPage(page, pageSize ,number);
        if (page >ordersServicePage.getPages()){
            ordersServicePage = ordersService.getPage((int) ordersServicePage.getPages(), pageSize ,number);
        }
        return Result.success(ordersServicePage);
    }
    @GetMapping("/userPage")
    public Result getUserPage(@RequestParam(value = "page") int page, @RequestParam(value = "pageSize")  int pageSize, @RequestParam(value = "number" ,required = false) Long number){
        IPage ordersServicePage = ordersService.getPage(page, pageSize ,number);
        if (page >ordersServicePage.getPages()){
            ordersServicePage = ordersService.getPage((int) ordersServicePage.getPages(), pageSize ,number);
        }
        return Result.success(ordersServicePage);
    }
}
